package com.ztools.mvp;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.ztools.base.BaseActivity;
import com.ztools.base.BaseFragment;
import com.ztools.mvp.proxy.MvpBindCallback;
import com.ztools.mvp.proxy.impl.FragmentLifecycleCallbackProxy;


/**
 * <pre>
 *    author: summer
 *    time  : 2018/03/06
 * </pre>
 */

public abstract class BaseMvpFragment<V extends IView, P extends IPresenter<V>> extends BaseFragment implements MvpBindCallback<V, P> {


    private P p;
    private V v;

    //获取目标接口的引用
    private FragmentLifecycleCallbackProxy<V, P> mLifeCallbackProxy;

    public FragmentLifecycleCallbackProxy<V, P> getLifeCallbackProxy() {
        if (this.mLifeCallbackProxy == null) {
            this.mLifeCallbackProxy = new FragmentLifecycleCallbackProxy<>(this);
        }
        return this.mLifeCallbackProxy;
    }

    @Override
    public P newPresenter() {
        return this.p;
    }

    @Override
    public V newMvpView() {
        return this.v;
    }

    @Override
    public P getPresenter() {
        return this.p;
    }

    @Override
    public void setPresenter(P p) {
        this.p = p;
    }

    @Override
    public V getMvpView() {
        return this.v;
    }

    @Override
    public void setMvpView(V v) {
        this.v = v;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        getLifeCallbackProxy().onCreate(savedInstanceState);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onAttach(Context context) {
        getLifeCallbackProxy().onAttach();
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        getLifeCallbackProxy().onViewCreated(view, savedInstanceState);
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onStart() {
        getLifeCallbackProxy().onStart();
        super.onStart();
    }

    @Override
    public void onPause() {
        getLifeCallbackProxy().onPause();
        super.onPause();
    }

    @Override
    public void onResume() {
        getLifeCallbackProxy().onResume();
        super.onResume();
    }

    @Override
    public void onStop() {
        getLifeCallbackProxy().onStop();
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        getLifeCallbackProxy().onDestroyView();
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        getLifeCallbackProxy().onDestroy();
        super.onDestroy();
    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }

    @Override
    public void bindEvent() {

    }

    @Nullable
    @Override
    public Context getContext() {
        if (super.getContext() instanceof BaseActivity) {
            return super.getContext();
        } else {
            return getActivity();
        }
    }
}
